package com.ansion.commonchat.contact.ui;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity;

import com.ansion.basedb.dao.LoginedUserMgr;
import com.ansion.basedb.dao.bean.SyncAdapterColumns;
import com.ansion.basedb.dao.model.CurrentUser;
import com.ansion.commonchat.R;
import com.ansion.commonchat.contact.sync.ContactManager;
import com.ansion.log.VLog;
import com.ansion.utils.ContactUtil;
import com.ansion.utils.permission.RealRxPermission;
import com.google.common.eventbus.Subscribe;

public class ChatProfileActivity extends FragmentActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			Window window = this.getWindow();
			//兼容5.0及以上支持全透明
			window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
			window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
			window.setStatusBarColor(Color.TRANSPARENT);
		}

//		EventBus.getDefault().register(this);
		VLog.d("ChatProfileActivity", "onCreate");
		Intent i = getIntent();
		dealContactShorCut(i);
		if (i==null) {
			finish();
			return;
		}

	}

	@Override
	protected void onDestroy() {
		try {
//			EventBus.getDefault().unregister(this);
		} catch (Exception e) {
		}
		super.onDestroy();
	}

	private void dealContactShorCut(Intent intent) {

		Uri data=intent.getData();
		// 处理无contact permission报crash的问题
		if (!RealRxPermission.getInstance().isGranted(Manifest.permission.READ_CONTACTS)) {
			return;
		}
		final long uid = ContactManager.getCocoUserIdByUri(this, data);
		String type =intent.getType();

		if (TextUtils.isEmpty(type)) {
			type=ContactManager.getCocoTypeByUri(this, data);
		}

		VLog.d("ChatProfileActivity", "uid:"+uid+" type: "+type);


		if (ContactUtil.INVALID_ID == uid) {
			Toast.makeText(this, getString(R.string.baba_baba), Toast.LENGTH_LONG);
//			HelperFunc.startTabHostPage(this, MainPageConfig.TAB_SESSION);
			finish();
			return;
		}
		CurrentUser user = LoginedUserMgr.getCurrentLoginedUser();
		if (user==null||user.getUserId()==uid) {
//			HelperFunc.startTabHostPage(this, MainPageConfig.TAB_SESSION);
			finish();
			return;
		}

		boolean isNeedFishActivity=true;
		if (SyncAdapterColumns.MIME_CHAT.equals(type)||SyncAdapterColumns.MIME_PROFILE.equals(type)) {
//					ModuleApiFactory.getChatApi().openChatActivity(this, String.valueOf(uid));
		}else if (SyncAdapterColumns.MIME_CALL_VOICE.equals(type)) {
//					if (VoipUtil.isSupportVoice()) {
//						if (!BlockHelper.isBlock(uid)) {
//							isNeedFishActivity = false;
//							ChatUtil.doActualDialAction(ChatProfileActivity.this, uid, RtcChatMessage.TYPE_VOICE, VoipConstants.VOIPFROM.VOIPFROM_CHATPROFILE);
//						}else {
//                            ModuleApiFactory.getChatApi().openChatActivity(this, String.valueOf(uid));
//						}
//
//					}else {
//
//						showUnactivateCallDialog(uid);
//						isNeedFishActivity=false;
//					}

		}else if (SyncAdapterColumns.MIME_CALL_VIDEO.equals(type)) {

//					if (VoipUtil.isSupportVideo()) {
//						if (!BlockHelper.isBlock(uid)) {
//							isNeedFishActivity = false;
//							ChatUtil.doActualDialAction(ChatProfileActivity.this, uid, RtcChatMessage.TYPE_VIDEO, VoipConstants.VOIPFROM.VOIPFROM_CHATPROFILE);
//						}else {
//                            ModuleApiFactory.getChatApi().openChatActivity(this, String.valueOf(uid));
//						}
//					}else {
//						showUnactivateCallDialog(uid);
//						isNeedFishActivity=false;
//					}
		}

		if (isNeedFishActivity) {
			finish();
		}

	}

	AlertDialog alertDialog=null;
	private void showUnactivateCallDialog(final long uid){

//		 if (alertDialog != null&&alertDialog.isShowing()) {
//	            alertDialog.dismiss();
//	            alertDialog = null;
//	        }
//
//	        alertDialog = CocoAlertDialog.newBuilder(ChatProfileActivity.this)
//					.setTitle(R.string.NotificationAlert)
//	                .setMessage(R.string.baba_carrieroff_voice)
//	                .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
//	                    public void onClick(DialogInterface dialog, int id) {
//	                    	alertDialog.dismiss();
//	                    }
//	                })
//	                .setCancelable(false).setOnKeyListener(new OnKeyListener() {
//
//	                    @Override
//	                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//	                        if (keyCode == KeyEvent.KEYCODE_BACK) {
//	                            // finish();
//	                            return true;
//	                        }
//	                        return false;
//	                    }
//	                }).create();
//	        alertDialog.show();
//	        CocoAlertDialog.setDialogStyle(alertDialog);
	}

	private static void showUserInfo(Context context, long uid) {
//		ChatUtil.showUserInfo(context, uid);
	}

//	@Subscribe(threadMode = ThreadMode.MAIN)
//	public void closeActivity(CloseProfileActivityEvent event) {
//		finish();
//	}

}
